package com.siren.tank;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : wangjichao
 * @description:
 * @date : 2020/7/26 16:47
 */
public class TankFrame extends Frame {


    public static final int WIDTH = 1600;
    public static final int HEIGHT = 1000;

    public TankFrame() {
        setSize(WIDTH, HEIGHT);
        setResizable(false);
        setTitle("tank war");
        setVisible(true);

        this.addKeyListener(new MyKeyListener());

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(-1);
            }
        });
    }

    Image offScreenImage = null;
    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(WIDTH, HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color color = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, WIDTH, HEIGHT);
        gOffScreen.setColor(color);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        GameModel.getInstance().paint(g);
    }

    class MyKeyListener extends KeyAdapter {

        Map<Integer, Boolean> keyEvents =new HashMap<>();

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("key pressed");
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    keyEvents.put(KeyEvent.VK_LEFT, true);
                    break;
                case KeyEvent.VK_RIGHT:
                    keyEvents.put(KeyEvent.VK_RIGHT, true);
                    break;
                case KeyEvent.VK_UP:
                    keyEvents.put(KeyEvent.VK_UP, true);
                    break;
                case KeyEvent.VK_DOWN:
                    keyEvents.put(KeyEvent.VK_DOWN, true);
                    break;
                case KeyEvent.VK_CONTROL:
                    keyEvents.put(KeyEvent.VK_CONTROL, true);
                    break;
                default:
                    break;
            }
            GameModel.getInstance().control(keyEvents);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            int keyCode = e.getKeyCode();
            switch (keyCode) {
                case KeyEvent.VK_LEFT:
                    keyEvents.put(KeyEvent.VK_LEFT, false);
                    break;
                case KeyEvent.VK_RIGHT:
                    keyEvents.put(KeyEvent.VK_RIGHT, false);
                    break;
                case KeyEvent.VK_UP:
                    keyEvents.put(KeyEvent.VK_UP, false);
                    break;
                case KeyEvent.VK_DOWN:
                    keyEvents.put(KeyEvent.VK_DOWN, false);
                    break;
                case KeyEvent.VK_CONTROL:
                    keyEvents.put(KeyEvent.VK_CONTROL, false);
                    break;
                default:
                    break;
            }
            GameModel.getInstance().control(keyEvents);
        }
    }



}
